<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/component/pear/css/pear.css"/>
    <link rel="stylesheet" href="/admin/css/other/console1.css"/>
    <link rel="stylesheet" href="/layer-css.css"/>
    <style>
        .centered {
            display: flex;
            flex-direction: row;
            align-items: center;
        }

        .pear-container {
            margin: 15px;
            width: calc(100vw - 30px);
        }

        .manualSwitch {
            width: 198px;
            height: 68px;
            background: #F3F5F8;
            border-radius: 8px 8px 8px 8px;
        }

        .layui-form-label {
            width: 0px;
        }

        .layui-input-block {
            margin-left: 45px;
            width: 260px;
        }

        .layui-form {
            font-size: 16px;
        }


    </style>
</head>
<body class="pear-container">
<div class="layui-row platformOverview layui-col-space15">
    <div class="layui-col-xs12 layui-col-md12">
        <div class="layui-card">
            <div class="layui-card-body">
                <div class="layui-row centered" style="margin-left: 12px;margin-top: 6px;">
                    <div style="width: 4px;height: 20px;background: #2176FF;"></div>
                    <div style="margin-left: 10px;height: 27px;font-weight: 500;font-size: 18px;color: #000000;line-height: 27px;">
                        手动操作
                    </div>
                </div>
                <div class="layui-row"
                     style="display: flex;flex-direction: column;margin-top: 5px;margin-left: 12px;margin-right: 12px;">
                    <img src="/img/console/网格.png" alt="" style="max-width: 100%;">
                </div>
                <form class="layui-form" action="" onsubmit="return false" lay-filter="selectLayerColumn">
                    <div class="layui-form-item">
                        <label class="layui-form-label">层</label>
                        <div class="layui-input-block">
                            <select name="layer" lay-search>
                                <option value="">请选择层</option>
                                <option th:each="layer : ${layerList}" th:value="${layer}" th:text="${layer}">
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">列</label>
                        <div class="layui-input-block">
                            <select name="column" lay-search>
                                <option value="">请选择列</option>
                                <option th:each="column : ${columnList}" th:value="${column}" th:text="${column}">
                            </select>
                        </div>
                    </div>
                </form>
                <div class="centered" style="margin-left: 12px;margin-top: 25px;">
                    <div class="manualSwitch centered" id="turnOnTheLight">
                        <img src="/img/手动开灯.svg" alt="" style="max-width: 100%;padding-left: 10px">
                        <span style="font-size: 16px;color: #2A3239;margin-left: 10px;">手动开灯</span>
                    </div>
                    <div class="manualSwitch centered" style="margin-left: 40px;" id="turnOffTheLights">
                        <img src="/img/手动关灯.svg" alt="" style="max-width: 100%;padding-left: 10px">
                        <span style="font-size: 16px;color: #2A3239;margin-left: 10px;">手动关灯</span>
                    </div>
<!--                    <div class="manualSwitch centered" style="margin-left: 40px;" id="turnOnAllLights">-->
<!--                        <img src="/img/打开所有灯.svg" alt="" style="max-width: 100%;padding-left: 10px">-->
<!--                        <span style="font-size: 16px;color: #2A3239;margin-left: 10px;">自检模式</span>-->
<!--                    </div>-->
                    <div class="manualSwitch centered" style="margin-left: 40px;" id="turnOffAllLights">
                        <img src="/img/打开所有灯.svg" alt="" style="max-width: 100%;padding-left: 10px">
                        <span style="font-size: 16px;color: #2A3239;margin-left: 10px;">关闭所有灯</span>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="layui-row layui-col-space10">
    <div class="layui-col-md12">
        <div class="layui-card">
            <div class="layui-card-body">
                <div class="layui-row centered" style="margin-left: 12px;margin-top: 6px;">
                    <div style="width: 4px;height: 20px;background: #2176FF;"></div>
                    <div style="margin-left: 10px;height: 27px;font-weight: 500;font-size: 18px;color: #000000;line-height: 27px;">
                        机柜配置
                    </div>
                </div>
                <div class="layui-row"
                     style="display: flex;flex-direction: column;margin-top: 5px;margin-left: 12px;margin-right: 12px;">
                    <img src="/img/console/网格.png" alt="" style="max-width: 100%;">
                </div>
                <table id="host-table" lay-filter="host-table"></table>
            </div>
        </div>
    </div>
</div>
<div class="layui-row layui-col-space10">
    <div class="layui-col-md12">
        <div class="layui-card">
            <div class="layui-card-body">
                <div class="layui-row centered" style="margin-left: 12px;margin-top: 6px;">
                    <div style="width: 4px;height: 20px;background: #2176FF;"></div>
                    <div style="margin-left: 10px;height: 27px;font-weight: 500;font-size: 18px;color: #000000;line-height: 27px;">
                        开灯时间
                    </div>
                </div>
                <div class="layui-row"
                     style="display: flex;flex-direction: column;margin-top: 5px;margin-left: 12px;margin-right: 12px;">
                    <img src="/img/console/网格.png" alt="" style="max-width: 100%;">
                </div>
                <form class="layui-form" action="" onsubmit="return false">
                    <div class="layui-form-item" style="margin-bottom: 5px;">
                        <label class="layui-form-label" style="width: 55px;">时间</label>
                        <div class="layui-input-block" style="margin-left: 90px;">
                            <input type="number" name="second" required lay-verify="required" lay-reqtext="请输入亮灯秒数"
                                   th:value="${sysParamset.getParamValue()}" placeholder="请输入亮灯秒数"
                                   autocomplete="off" class="layui-input">
                            <div class="layui-form-mid layui-text-em">单位是秒</div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block" style="margin-left: 90px;">
                            <button class="layui-btn" lay-submit lay-filter="secondSave">保存</button>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="role-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i
            class="layui-icon layui-icon-edit"></i></button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i
            class="layui-icon layui-icon-delete"></i></button>
</script>
<script type="text/html" id="host-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
</script>

<script src="/component/layui/layui.js"></script>
<script src="/component/pear/pear.js"></script>
<script th:inline="javascript">
    layui.use(['form', 'jquery', 'table'], function () {
        let form = layui.form;
        let $ = layui.jquery;
        let table = layui.table;

        $("#turnOnTheLight").click(function () {
            var data = form.val('selectLayerColumn');
            if (data.layer === "") {
                layer.msg('请选择层', {icon: 2, time: 1800});
                return;
            }
            if (data.column === "") {
                layer.msg('请选择列', {icon: 2, time: 1800});
                return;
            }
            let loading = layer.load();
            $.ajax({
                url: '/sys/paramset/lightOn',
                data: JSON.stringify(data),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function (result) {
                    layer.close(loading);
                    if (result.code === 200) {
                        layer.msg(result.message, {icon: 1, time: 1800});
                    } else {
                        layer.msg(result.message, {icon: 2, time: 1800});
                    }
                }
            })
        });
        $("#turnOffTheLights").click(function () {
            var data = form.val('selectLayerColumn');
            if (data.layer === "") {
                layer.msg('请选择层', {icon: 2, time: 1800});
                return;
            }
            if (data.column === "") {
                layer.msg('请选择列', {icon: 2, time: 1800});
                return;
            }
            let loading = layer.load();
            $.ajax({
                url: '/sys/paramset/turnOffTheLights',
                data: JSON.stringify(data),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function (result) {
                    layer.close(loading);
                    if (result.code === 200) {
                        layer.msg(result.message, {icon: 1, time: 1800});
                    } else {
                        layer.msg(result.message, {icon: 2, time: 1800});
                    }
                }
            })
        });

        $("#turnOffAllLights").click(function () {
            let loading = layer.load();
            $.ajax({
                url: '/sys/paramset/turnOffAllLights',
                dataType: 'json',
                type: 'get',
                success: function (result) {
                    layer.close(loading);
                    if (result.code === 200) {
                        layer.msg(result.message, {icon: 1, time: 1800});
                    } else {
                        layer.msg(result.message, {icon: 2, time: 1800});
                    }
                }
            })
        });
        $("#turnOnAllLights").click(function () {
            let loading = layer.load();
            $.ajax({
                url: '/sys/paramset/turnOnAllLights',
                dataType: 'json',
                type: 'get',
                success: function (result) {
                    layer.close(loading);
                    if (result.code === 200) {
                        layer.msg(result.message, {icon: 1, time: 1800});
                    } else {
                        layer.msg(result.message, {icon: 2, time: 1800});
                    }
                }
            })
        });

        let cols = [
            [
                {title: '主机ip', field: 'ip', align: 'center'},
                {title: '编号', field: 'number', align: 'center'},
                {title: '层', field: 'numberOfLayers', align: 'center'},
                {title: '列', field: 'numberOfColumns', align: 'center'},
                {title: '操作', toolbar: '#role-bar', align: 'center', width: 130}
            ]
        ]

        tableIns = table.render({
            elem: '#host-table',
            url: '/host/ip/list',
            page: true,
            cols: cols,
            skin: 'line',
            toolbar: '#host-toolbar',
            id: 'host-table',
            //解决页码的问题
            done: function (res) {
                // 获得当前页码
                var brforeCurr = tableIns.config.page.curr;
                // 获得当前页的记录数
                var dataLength = table.cache['host-table'].length;
                var count = res.count; // 获得总记录数
                //如果当前页的记录数为0并且总记录数不为0
                if (dataLength == 0 && count != 0) {
                    // 刷新表格到上一页
                    table.reload("host-table", {
                        page: {
                            curr: brforeCurr - 1
                        }
                    });
                }
            }
        })

        table.on('toolbar(host-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            }
        });

        // 新增机柜
        window.add = function () {
            layer.open({
                title: ['新增机柜', 'font-size: 18px;'],
                type: 2,
                offset: 'r',
                anim: 'slideLeft', // 从右往左
                area: ['40%', '100%'],
                shade: 0.5,
                shadeClose: true,
                maxmin: true,
                id: 'addNewEquipment',
                content: '/host/add',
                end: function () {
                    table.reload("host-table")
                }
            });
        }

        table.on('tool(host-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        });
        //主机信息修改
        window.edit = function (obj) {
            var data = obj.data;
            layer.open({
                title: ['修改机柜', 'font-size: 18px;'],
                type: 2,
                offset: 'r',
                anim: 'slideLeft', // 从右往左
                area: ['40%', '100%'],
                shade: 0.5,
                shadeClose: true,
                maxmin: true,
                id: 'addNewEquipment',
                content: '/host/edit/?hostId=' + data.id,
                end: function () {
                    table.reload("host-table")
                }
            });
        }

        // 单行删除功能
        window.remove = function (obj) {
            layer.confirm('<div>确定要删除该主机吗？</div>', {
                icon: 3,
                title: ['删除确认', 'font-size: 18px;'],
                shade: 0.5,
                area: ['499px', '303px'],
                id: "borrowingConfirm"
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/host/remove/" + obj.data['id'],
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code === 200) {
                            layer.msg(result.message, {icon: 1, time: 1800}, function () {
                                obj.del();
                                table.reload("host-table");
                            });
                        } else {
                            layer.msg(result.message, {icon: 2, time: 1800});
                        }
                    }
                })
            });
        }
        // 提交事件
        form.on('submit(secondSave)', function (data) {
            var field = data.field;
            let loading = layer.load();
            $.ajax({
                url: '/sys/paramset/second/save',
                data: JSON.stringify(field),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function (result) {
                    layer.close(loading);
                    if (result.code === 200) {
                        layer.msg(result.message, {icon: 1, time: 1800});
                    } else {
                        layer.msg(result.message, {icon: 2, time: 1800});
                    }
                }
            })
            return false; // 阻止默认 form 跳转
        });
    });

</script>
</body>
</html>